package com.whcoding.mapstruct.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whcoding.mapstruct.dto.MemberDto;
import com.whcoding.mapstruct.dto.MemberOrderDto;
import com.whcoding.mapstruct.dto.OrderDto;
import com.whcoding.mapstruct.dto.ProductDto;
import com.whcoding.mapstruct.exception.ProductValidatorException;
import com.whcoding.mapstruct.mapper.*;
import com.whcoding.mapstruct.po.MemberVO;
import com.whcoding.mapstruct.po.OrderVO;
import com.whcoding.mapstruct.po.ProductVO;
import com.whcoding.mapstruct.util.LocalJsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * MapStruct对象转换测试Controller
 */
@RestController
@RequestMapping("/mapStruct")
public class MapStructController {

    @Autowired
    private MemberSpringMapper memberSpringMapper;

    /**
     * 基本映射
     */
    @GetMapping("/baseMapping")
    public void baseTest() {
        List<MemberVO> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberVO.class);
        MemberDto memberDto = MemberMapper.INSTANCE.toDto(memberList.get(0));
    }

    /**
     * 集合映射
     */
    @GetMapping("/collectionMapping")
    public void collectionMapping() {
        List<MemberVO> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberVO.class);
        List<MemberDto> memberDtoList = MemberMapper.INSTANCE.toDtoList(memberList);
        System.out.println("collectionMapping 返回结果"+JSON.toJSON(memberDtoList));
    }

    /**
     * 子对象映射
     */
    @GetMapping("/subMapping")
    public void subMapping() {
        List<OrderVO> orderList = getOrderList();
        OrderDto orderDto = OrderMapper.INSTANCE.toDto(orderList.get(0));
    }

    /**
     * 组合映射
     *
     */
    @GetMapping("/compositeMapping")
    public void compositeMapping() {
        List<OrderVO> orderList = LocalJsonUtil.getListFromJson("json/orders.json", OrderVO.class);
        List<MemberVO> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberVO.class);
        MemberVO member = memberList.get(0);
        OrderVO order = orderList.get(0);
        MemberOrderDto memberOrderDto = MemberMapper.INSTANCE.toMemberOrderDto(member,order);
    }

    /**
     * 使用依赖注入
     */
    @GetMapping("/springMapping")
    public void springMapping() {
        List<MemberVO> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberVO.class);
        MemberDto memberDto = memberSpringMapper.toDto(memberList.get(0));
    }

    /**
     * 使用常量、默认值和表达式
     */
    @GetMapping("/defaultMapping")
    public void defaultMapping() {
        List<ProductVO> productList = LocalJsonUtil.getListFromJson("json/products.json", ProductVO.class);
        ProductVO product = productList.get(0);
        product.setId(100L);
        product.setCount(null);
        ProductDto productDto = ProductMapper.INSTANCE.toDto(product);
    }

    /**
     * 在映射前后进行自定义处理
     */
    @GetMapping("/customRoundMapping")
    public void customRoundMapping() {
        List<ProductVO> productList = LocalJsonUtil.getListFromJson("json/products.json", ProductVO.class);
        ProductVO product = productList.get(0);
        product.setPrice(new BigDecimal(-1));
        ProductDto productDto = ProductRoundMapper.INSTANCE.toDto(product);
    }

    /**
     * 处理映射异常
     */
    @GetMapping("/exceptionMapping")
    public void exceptionMapping() {
        List<ProductVO> productList = LocalJsonUtil.getListFromJson("json/products.json", ProductVO.class);
        ProductVO product = productList.get(0);
        product.setPrice(new BigDecimal(-1));
        ProductDto productDto = null;
        try {
            productDto = ProductExceptionMapper.INSTANCE.toDto(product);
        } catch (ProductValidatorException e) {
            e.printStackTrace();
        }

    }

    private List<OrderVO> getOrderList() {
        List<OrderVO> orderList = LocalJsonUtil.getListFromJson("json/orders.json", OrderVO.class);
        List<ProductVO> productList = LocalJsonUtil.getListFromJson("json/products.json", ProductVO.class);
        List<MemberVO> memberList = LocalJsonUtil.getListFromJson("json/members.json", MemberVO.class);
        for (int i = 0; i < orderList.size(); i++) {
            OrderVO order = orderList.get(i);
            order.setMemberVO(memberList.get(i));
            order.setProductList(productList);
        }
        return orderList;
    }
}
